<template>
  <el-dialog
    :title="!dataForm.leaveId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="失踪人口id" prop="missId">
      <el-input v-model="dataForm.missId" placeholder="失踪人口id"></el-input>
    </el-form-item>
    <el-form-item label="留言信息" prop="leaveMessage">
      <el-input v-model="dataForm.leaveMessage" placeholder="留言信息"></el-input>
    </el-form-item>
    <el-form-item label="留言时间" prop="leaveTime">
      <el-input v-model="dataForm.leaveTime" placeholder="留言时间"></el-input>
    </el-form-item>
    <el-form-item label="留言的用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="留言的用户id"></el-input>
    </el-form-item>
    <el-form-item label="留言的用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="留言的用户名"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          leaveId: 0,
          missId: '',
          leaveMessage: '',
          leaveTime: '',
          userId: '',
          username: ''
        },
        dataRule: {
          missId: [
            { required: true, message: '失踪人口id不能为空', trigger: 'blur' }
          ],
          leaveMessage: [
            { required: true, message: '留言信息不能为空', trigger: 'blur' }
          ],
          leaveTime: [
            { required: true, message: '留言时间不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '留言的用户id不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '留言的用户名不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.leaveId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.leaveId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/leavemessage/info/${this.dataForm.leaveId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.missId = data.leaveMessage.missId
                this.dataForm.leaveMessage = data.leaveMessage.leaveMessage
                this.dataForm.leaveTime = data.leaveMessage.leaveTime
                this.dataForm.userId = data.leaveMessage.userId
                this.dataForm.username = data.leaveMessage.username
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/leavemessage/${!this.dataForm.leaveId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'leaveId': this.dataForm.leaveId || undefined,
                'missId': this.dataForm.missId,
                'leaveMessage': this.dataForm.leaveMessage,
                'leaveTime': this.dataForm.leaveTime,
                'userId': this.dataForm.userId,
                'username': this.dataForm.username
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
